<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';


class dao_booking_line {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT * "
                . "FROM tbl_booking_line ";
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function save($id, $booking_id, $row_pos, 
                $column_pos) {
        $db = new connection();
        $con = $db->open_connect();
        
        if ($id == 0) {
            $query = "INSERT INTO tbl_booking_line(booking_id, row_pos, column_pos) VALUES (
                " . $booking_id . ",'" . $row_pos . "','" . $column_pos . "')";
        } else {
            $query = "UPDATE tbl_booking_line SET "
                    . "booking_id = '" . $booking_id . "',"
                    . "row_pos = " . $row_pos . ","
                    . "column_pos = '" . $column_pos . "' "
                    . "WHERE id = " . $id;
        }
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
        $inserted_id = TRUE;
        if ($id == 0) {
            $inserted_id = mysqli_insert_id($db->con);
        }
        $db->close_connect();
        return $inserted_id;
    }

    public function get_by_id($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * "
                . "FROM tbl_booking_line "
                . "WHERE id = " . $id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function get_all_by_booking_id($booking_id, $user_id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT bl.* "
                . "FROM tbl_booking_line bl "
                . "LEFT JOIN tbl_booking b ON bl.booking_id = b.id "
                . "WHERE booking_id = " . $booking_id . " "
                . "AND b.user_id = " . $user_id;
        
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }
    
    public function get_by_position($booking_id, $row_pos, $column_pos) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * "
                . "FROM tbl_booking_line "
                . "WHERE booking_id = " . $booking_id . " "
                . "AND row_pos = '" . $row_pos . "' "
                . "AND column_pos = '" . $column_pos . "' ";
        
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }
    
    public function delete($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_booking_line WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
    
    public function delete_by_booking_id($booking_id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_booking_line "
                . "WHERE booking_id = " . $booking_id . " ";
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
    
}
